package code

import (
	"context"
	"fmt"

	"github.com/docker/docker/api/types/container"
)

// 提交容器
func ContainerCommit() {
	cli := InitClient()

	ctx := context.Background()
	createResp, err := cli.ContainerCreate(ctx, &container.Config{
		Image: "alpine",
		Cmd:   []string{"touch", "/helloworld"},
	}, nil, nil, nil, "")

	if err != nil {
		panic(err)
	}

	if err := cli.ContainerStart(ctx, createResp.ID, container.StartOptions{}); err != nil {
		panic(err)
	}

	statusCh, errCh := cli.ContainerLogs(ctx, createResp.ID, container.WaitConditionNotRunning)
	select {
	case err := <-errCh:
		if err != nil {
			panic(err)
		}
	case <-statusCh:
	}

	commitResp, err := cli.ContainerCommit(ctx, createResp.ID, container.CommitOptions{Reference: "myalpine"})
	if err != nil {
		panic(err)
	}
	fmt.Printf("commitResp: %#v\n", commitResp)
}
